layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var brandId = $("#brandId").val();
    var activityId = $("#activityId").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/item/jsonList',
        where: { activityId: activityId},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'title',minWidth:120, title: '商品名称',align:'center'},
            {field: 'brandName',minWidth:120, title: '品牌名称',align:'center'},
            {field: 'createTime',minWidth:170, title: '创建时间', align:'center', templet:function(d){
                return d.createTime;
            }},
            {title: '操作',width:170,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">添加</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                title: $("#title").val(),
                activityId: $("#activityId").val()
            }
        })
    });

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'del'){
            $.post("/activityItem/updateOrAddEntity",{
                itemId : data.id,
                activityId : $("#activityId").val()
            },function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    // parent.tableIns.reload();
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                }
            },"json")
        }
    });
})